package repository

import (
	"gorm.io/gorm"
	"shop-bean/model"
	"shop-common/utils/queryWrapper"
)

type IAreaRepository interface {
	Create(db *gorm.DB, area *model.Area) error
	Delete(db *gorm.DB, id uint) error
	Updates(db *gorm.DB, id uint, columns map[string]interface{}) error
	Find(db *gorm.DB, pair *queryWrapper.WherePair) ([]*model.Area, error)
}

type AreaRepository struct {
}

func NewAreaRepository() IAreaRepository {
	return &AreaRepository{}
}

func (r *AreaRepository) Create(db *gorm.DB, area *model.Area) error {
	return db.Create(&area).Error
}

func (r *AreaRepository) Delete(db *gorm.DB, id uint) error {
	return db.Delete(&model.Area{}, id).Error
}

func (r *AreaRepository) Updates(db *gorm.DB, id uint, columns map[string]interface{}) error {
	return db.Model(&model.Area{}).Where("area_id = ?", id).Updates(columns).Error
}

func (r *AreaRepository) Find(db *gorm.DB, pair *queryWrapper.WherePair) (list []*model.Area, err error) {
	tx := db.Model(&model.Area{})
	tx = queryWrapper.ReuseWherePair(tx, pair)
	return list, tx.Find(&list).Error
}
